package cn.kill.service.impl;

import cn.jbit.entity.User;
import cn.jbit.utils.Salt;
import cn.kill.mapper.UserMapper;
import cn.kill.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * 源码学院只为培养优秀的bat程序员而生
 *
 * @author 学员张在成
 * @data 2021/6/14
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User UserInfoAsUserPwd(String username, String password) {

        User user = userMapper.UserInfoAsUserPwd(username, password);
        if (user!=null){
            log.info("用户登录成功");
        }else {
            log.info("您的账户登录失败");
        }
        return user;
    }

    @Override
    public int addUser(User userInfo) {
        User user=new User();
        user.setUsername(userInfo.getUsername());
        user.setPassword(DigestUtils.md5Hex(Salt.SALT+userInfo.getPassword()));
        user.setIsOpen(userInfo.getIsOpen());
        user.setIsVip(userInfo.getIsVip());
        user.setLevel(userInfo.getLevel());
        user.setPhone(userInfo.getPhone());
        user.setUserPicture(userInfo.getUserPicture());
        int i = userMapper.addUser(user);

        if (i>0){
            log.info("注册成功："+i);
        }

        return i;
    }

    @Override
    public User getByUsername(String username) {
        return userMapper.getByUsername(username);
    }
}
